package com.cmic.rcs.emoji.service.impl;

import com.cmic.rcs.emoji.common.UserContext;
import com.cmic.rcs.emoji.entity.pojo.TDataConfig;
import com.cmic.rcs.emoji.mapper.DataConfigMapper;
import com.cmic.rcs.emoji.service.DataConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * 数据配置服务实现类
 * 
 * @author 系统生成
 * @since 2024-01-01
 */
@Service
@Slf4j
public class DataConfigServiceImpl implements DataConfigService {

    @Autowired
    private DataConfigMapper dataConfigMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TDataConfig saveDataConfig(TDataConfig dataConfig) {
        // 参数校验
        if (dataConfig == null || StringUtils.isEmpty(dataConfig.getEnterpriseCode())) {
            throw new RuntimeException("企业编码不能为空");
        }
        
        if (dataConfig.getDataType() == null) {
            throw new RuntimeException("数据类型不能为空");
        }
        
        // 获取当前登录用户
        String currentUser = UserContext.getUserDto().getUsername();
        
        // 查询是否已存在该企业的该类型配置
        TDataConfig existingConfig = dataConfigMapper.selectByEnterpriseCodeAndDataType(
                dataConfig.getEnterpriseCode(), dataConfig.getDataType());
        
        if (existingConfig != null) {
            // 存在则更新
            existingConfig.setCostSavings(dataConfig.getCostSavings());
            existingConfig.setCycleReduction(dataConfig.getCycleReduction());
            existingConfig.setDataDescription(dataConfig.getDataDescription());
            existingConfig.setMessageSuccessRate(dataConfig.getMessageSuccessRate());
            existingConfig.setEmailSuccessRate(dataConfig.getEmailSuccessRate());
            existingConfig.setDocumentSuccessRate(dataConfig.getDocumentSuccessRate());
            existingConfig.setUpdateTime(LocalDateTime.now());
            existingConfig.setUpdateBy(currentUser);
            dataConfigMapper.updateById(existingConfig);
            log.info("更新数据配置成功，企业编码：{}", dataConfig.getEnterpriseCode());
            return existingConfig;
        } else {
            // 不存在则新增
            dataConfig.setCreateTime(LocalDateTime.now());
            dataConfig.setCreateBy(currentUser);
            dataConfig.setUpdateTime(LocalDateTime.now());
            dataConfig.setUpdateBy(currentUser);
            dataConfigMapper.insert(dataConfig);
            log.info("新增数据配置成功，企业编码：{}", dataConfig.getEnterpriseCode());
            return dataConfig;
        }
    }

    @Override
    public TDataConfig getDataConfigByEnterpriseCode(String enterpriseCode) {
        if (enterpriseCode == null) {
            throw new RuntimeException("企业编码不能为空");
        }
        return dataConfigMapper.selectByEnterpriseCode(enterpriseCode);
    }

    @Override
    public TDataConfig getDataConfigByEnterpriseCodeAndDataType(String enterpriseCode, String dataType) {
        if (enterpriseCode == null) {
            throw new RuntimeException("企业编码不能为空");
        }
        
        if (dataType == null) {
            throw new RuntimeException("数据类型不能为空");
        }

        return dataConfigMapper.selectByEnterpriseCodeAndDataType(enterpriseCode, dataType);
    }
}